using System;
using System.Windows;
using System.Windows.Controls;
namespace Microsoft.Phone.Controls.Primitives
{
	[StyleTypedProperty(Property = "ItemContainerStyle", StyleTargetType = typeof(MenuItem))]
	public abstract class MenuBase : ItemsControl
	{
		public static readonly DependencyProperty ItemContainerStyleProperty = DependencyProperty.Register("ItemContainerStyle", typeof(Style), typeof(MenuBase), null);
		public Style ItemContainerStyle
		{
			get
			{
				return (Style)base.GetValue(MenuBase.ItemContainerStyleProperty);
			}
			set
			{
				base.SetValue(MenuBase.ItemContainerStyleProperty, value);
			}
		}
		protected override bool IsItemItsOwnContainerOverride(object item)
		{
			return item is MenuItem || item is Separator;
		}
		protected override DependencyObject GetContainerForItemOverride()
		{
			return new MenuItem();
		}
		protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
		{
			base.PrepareContainerForItemOverride(element, item);
			MenuItem menuItem = element as MenuItem;
			if (menuItem != null)
			{
				menuItem.ParentMenuBase = this;
				if (menuItem != item)
				{
					DataTemplate itemTemplate = base.get_ItemTemplate();
					Style itemContainerStyle = this.ItemContainerStyle;
					if (itemTemplate != null)
					{
						menuItem.SetValue(ItemsControl.ItemTemplateProperty, itemTemplate);
					}
					if (itemContainerStyle != null && MenuBase.HasDefaultValue(menuItem, HeaderedItemsControl.ItemContainerStyleProperty))
					{
						menuItem.SetValue(HeaderedItemsControl.ItemContainerStyleProperty, itemContainerStyle);
					}
					if (MenuBase.HasDefaultValue(menuItem, HeaderedItemsControl.HeaderProperty))
					{
						menuItem.Header = item;
					}
					if (itemTemplate != null)
					{
						menuItem.SetValue(HeaderedItemsControl.HeaderTemplateProperty, itemTemplate);
					}
					if (itemContainerStyle != null)
					{
						menuItem.SetValue(FrameworkElement.StyleProperty, itemContainerStyle);
					}
				}
			}
		}
		private static bool HasDefaultValue(Control control, DependencyProperty property)
		{
			return control.ReadLocalValue(property) == DependencyProperty.UnsetValue;
		}
	}
}
